home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual Basic 5 / Mastering Microsoft Visual Basic 5.ISO / sampapps / globaltimer / modtimer.bas < prev    next >
BASIC Source File  |  1997-01-14  |  2KB  |  55 lines

  1. Attribute VB_Name = "modTimer"
  2. Option Explicit
  3.  
  4. Declare Function SetTimer Lib "user32" _
  5.   (ByVal hwnd As Long, ByVal nIDEvent As Long, _
  6.   ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
  7. Attribute SetTimer.VB_MemberFlags = "40"
  8. Declare Function KillTimer Lib "user32" _
  9.   (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
  10. Attribute KillTimer.VB_MemberFlags = "40"
  11.  
  12. ' This collection holds pointers to all of the CTimer objects
  13. ' that have active Window's timers. The key is the timerID belonging
  14. ' to the timer associated with the object.
  15. Private cTimers As Collection
  16.  
  17. ' This callback is shared among the different timers and can be
  18. ' distinguished from each other by the timer ID pass in. Its string
  19. ' representation is used as the key to get the associated CTimer object.
  20. Public Sub TimerProc(ByVal hwnd As Long, ByVal msg As Long, _
  21.   ByVal idEvent As Long, ByVal curTime As Long)
  22.   Dim tim As CTimer
  23.   
  24.   ' Find the timer that fired in the CTimers collection
  25.   Set tim = cTimers.Item(Str$(idEvent))
  26.   
  27.   If Not tim Is Nothing Then
  28.     ' Once the appropriate CTimer is found, call its Timer_Event function
  29.     ' so that it can raise the event to its clients.
  30.     tim.Timer_Event
  31.   Else
  32.     Debug.Print "Timer " & idEvent & " not found in collection."
  33.   End If
  34. End Sub
  35.  
  36. Public Sub Main()
  37.   ' Create the timers collection object.
  38.   Set cTimers = New Collection
  39. End Sub
  40.  
  41. Public Sub AddTimer(objTimer As CTimer)
  42. Attribute AddTimer.VB_MemberFlags = "40"
  43.   ' Add CTimer object to collection. Use the object's TimerID
  44.   ' as the search key (note that the key is of type String)
  45.   cTimers.Add Item:=objTimer, Key:=objTimer.TimerID
  46. End Sub
  47.  
  48. Public Sub RemoveTimer(objTimer As CTimer)
  49. Attribute RemoveTimer.VB_MemberFlags = "40"
  50.   ' The Window's timer associated with the CTimer object has been killed.
  51.   ' Using the object's timer ID as the key, remove the CTimer object
  52.   ' from the cTimers collection.
  53.   cTimers.Remove objTimer.TimerID
  54. End Sub
  55.